import config from '@/config.js'
import store from '@/store/index.js'
export default {
	SocketTask: '',
	socketOpen: false, //连接是否打开
	timer: null, //心跳
	again_timer: null, //重新连接
	init() {
		const token = uni.getStorageSync('token')
		if (token) {
			console.log(config.scoket)
			this.SocketTask = uni.connectSocket({
				url: config.scoket + '?token=' + token,
				success: (res) => {

				}
			})
			uni.onSocketOpen((res) => {
				this.socketOpen = true
				console.log('WebSocket连接已打开！')
				this.timer = setInterval(() => {
					this.send()
				}, 28000)

				if (this.again_timer != null) { //清除重新连接定时器
					clearInterval(this.again_timer)
				}
			})
			uni.onSocketError((res) => {
				this.socketOpen = false
				console.log('WebSocket连接打开失败，请检查！', res);
			})
			uni.onSocketClose((res) => {
				this.socketOpen = false
				console.log(res, 'WebSocket连接已关闭')
				if (!this.socketOpen && this.again_timer == null) {
					this.again_timer = setInterval(() => {
						this.init()
					}, 7000)
				}
			})
			// 监听scoket信息
			uni.onSocketMessage((res) => {
				// console.log('收到服务器内容', res)
				let msg = JSON.parse(res.data)
				console.log(msg, '收到服务器内容')
				if (msg.msgtype == 'session_list') { //获取消息列表
					if (msg.total > 0) {
						store.commit('set_new_msg_num', msg.total)
					}
					store.commit('set_sessionlist', msg.data) //提交vuex，同步聊天记录
					if (msg.system_notice) {
						store.commit('set_xt_tz', msg.system_notice) //提交vuex，系统通知
					}
					// this.set_sessionlist(msg.data)

				} else if (msg.msgtype == 'initialize' || msg.msgtype == 'chat_record') { //scoket初始化/获取聊天记录
					store.commit('set_chat_record', msg.data.chat_record) //提交vuex，同步聊天记录
					// 更新消息页聊天列表
					var send_msg = {
						c: 'Message',
						a: 'sessionList',
						data: {
							user_id: store.state.user_info.id
						}
					}
					this.send(send_msg)
				} else if (msg.msgtype == 'getSessionId') { //获取回话id
					store.commit('set_chat_id', msg.data.session_id) //提交vuex，回话id
					var message = {
						c: 'Message',
						a: 'chatRecord',
						data: {
							session_id: store.state.chat_id,
							user_id: store.state.user_info.id,
							csr_id: store.state.df_id
						}
					}
					this.send(message)
				} else if (msg.msgtype == 'new_message' || msg.msgtype == 'send_message') { //收到新消息，发送消息，
					// 更新消息页聊天列表
					var send_msg = {
						c: 'Message',
						a: 'sessionList',
						data: {
							user_id: store.state.user_info.id
						}
					}
					this.send(send_msg)

					// 更新聊天室记录
					let pages = getCurrentPages();
					let currentPage = pages[pages.length - 1]; // 当前页面路由
					if (currentPage.route == 'pages/msg/msg_room') {
						// 判断当前页面是否在聊天室
						var message = {
							c: 'Message',
							a: 'chatRecord',
							data: {
								session_id: store.state.chat_id,
								user_id: store.state.user_info.id,
								csr_id: store.state.df_id
							}
						}
						this.send(message)
					}
				} else if (msg.msgtype == "delSession" || msg.msgtype == "top") { //回话列表置顶删除
					// 更新消息页聊天列表
					var send_msg = {
						c: 'Message',
						a: 'sessionList',
						data: {
							user_id: store.state.user_info.id
						}
					}
					this.send(send_msg)
				} else if (msg.msgtype == 'system_notice') {
					// 获取系统通知列表
					store.commit('set_tz_list', msg.data)
					// 将系统通知设为已读
					var send_msg = {
						c: 'Message',
						a: 'readSystemNotice',
						data: {
							user_id: store.state.user_info.id
						}
					}
					this.send(send_msg)
				} else if (msg.msgtype == 'read_system_notice' || msg.msgtype == 'fh' || msg.msgtype ==
					'refund') {
					// 更新消息页聊天列表
					var send_msg = {
						c: 'Message',
						a: 'sessionList',
						data: {
							user_id: store.state.user_info.id
						}
					}
					this.send(send_msg)
				}
			})
		}
	},
	send(data) {
		if (!data) {
			data = {
				c: 'Message',
				a: 'ping'
			};
		}
		if (this.socketOpen) {
			uni.sendSocketMessage({
				data: JSON.stringify(data),
				success: (res) => {
					console.log(res)
				},
				fail: (res) => {

				}
			})
		}
	},
	close() {
		if (this.socketOpen) {
			this.socketOpen = false
			uni.closeSocket()
		}
	}

}